﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using DevExpress.XtraReports.UI;

namespace HoaDon
{
	public partial class BaoCao_NXT : System.Web.UI.Page
	{
		private System.Globalization.CultureInfo cultureinfo = new System.Globalization.CultureInfo("vi-VN");
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				dttuNgay.Text = DateTime.Now.ToString("dd/MM/yyyy");
				dtdenNgay.Text = DateTime.Now.ToString("dd/MM/yyyy");

				drnhomhang.DataSource = Get_NhomHang();
				drnhomhang.DataValueField = "Nhvt2ID";
				drnhomhang.DataTextField = "Nhvt2Name";
				drnhomhang.DataBind();
				drnhomhang.SelectedIndex = 0;

				drcuahang.DataSource = Get_CuaHang();
				drcuahang.DataValueField = "KhoID";
				drcuahang.DataTextField = "KhoName";
				drcuahang.DataBind();
				drcuahang.SelectedIndex = 0;

				drkhuvuc.DataSource = Get_KhuVuc();
				drkhuvuc.DataValueField = "NhvvID";
				drkhuvuc.DataTextField = "NhvvName";
				drkhuvuc.DataBind();
				drkhuvuc.SelectedIndex = 0;

				txtMaHangHoa.Text = "";
				Get_Ban(drkhuvuc.SelectedValue);
			}
		}


		protected void btIn_Click(object sender, EventArgs e)
		{
			string ngaybd_bc = "", ngaykt_bc = "", nhom_hang = "", ma_hang = "", ma_cuahang = "", ma_khuvuc = "", ma_ban = "", ngaybd = "", ngaykt = "";
			int ps = 0, ton = 0;
			try
			{
				if (!dttuNgay.Text.ToString().Equals(""))
				{
					ngaybd_bc = DateTime.Parse(dttuNgay.Text, cultureinfo).ToString("yyyyMMdd");
					ngaybd = DateTime.Parse(dttuNgay.Text, cultureinfo).ToString("dd/MM/yyyy");
				}
				else
					return;
				if (!dtdenNgay.Text.ToString().Equals(""))
				{
					ngaykt_bc = DateTime.Parse(dtdenNgay.Text, cultureinfo).ToString("yyyyMMdd");
					ngaykt = DateTime.Parse(dtdenNgay.Text, cultureinfo).ToString("dd/MM/yyyy");
				}
				else
					return;

				if (drnhomhang.SelectedValue.ToString() != "")
				{
					nhom_hang = drnhomhang.SelectedValue;
				}
				if (drkhuvuc.SelectedValue.ToString() != "")
				{
					ma_khuvuc = drkhuvuc.SelectedValue;
				}
				if (drcuahang.SelectedValue.ToString() != "")
				{
					ma_cuahang = drcuahang.SelectedValue;
				}
				if (drban.SelectedValue.ToString() != "")
				{
					ma_ban = drban.SelectedValue;
				}
				if (ckmathangton.Checked)
					ton = 1;
				if (ckps.Checked)
					ps = 1;
				ma_hang = txtMaHangHoa.Text.Trim();


				DataTable dtReport = new DataTable();
				string s = "EXEC [fs_HG_BCTHNXT2] '" + ngaybd_bc + "', '" + ngaykt_bc + "', N'" + ma_hang + "', N'" + nhom_hang + "', N'" + ma_cuahang + "', N'', N'" + ma_ban + "', N'" + ma_khuvuc + "', " + ps + ", " + ton + ", N'" + Session["FK_DatabaseID"].ToString() + "'";
				dtReport = SqlEXEC.GetData(Session["strConn"].ToString(), s);
				rptTHNXT rpt = new rptTHNXT { DataSource = dtReport };
				rpt.Ma_hang.DataBindings.Add("Text", dtReport, "Ma_vt");
				rpt.Ten_hang.DataBindings.Add("Text", dtReport, "Ten_vt");
				rpt.Dvt.DataBindings.Add("Text", dtReport, "Dvt");
				rpt.Ton_dau.DataBindings.Add("Text", dtReport, "Ton_dau", "{0:n0}");
				rpt.Du_dau.DataBindings.Add("Text", dtReport, "Du_dau", "{0:n0}");
				rpt.SL_Nhap.DataBindings.Add("Text", dtReport, "sl_nhap", "{0:n0}");
				rpt.Tien_nhap.DataBindings.Add("Text", dtReport, "Tien_nhap", "{0:n0}");
				rpt.Sl_Xuat.DataBindings.Add("Text", dtReport, "sl_xuat", "{0:n0}");
				rpt.Tien_xuat.DataBindings.Add("Text", dtReport, "Tien_xuat", "{0:n0}");
				rpt.Ton_cuoi.DataBindings.Add("Text", dtReport, "Ton_cuoi", "{0:n0}");
				rpt.Du_cuoi.DataBindings.Add("Text", dtReport, "Du_cuoi", "{0:n0}");
				rpt.Parameters["tungay"].Value = ngaybd;
				rpt.Parameters["denngay"].Value = ngaykt;

				if (drcuahang.SelectedValue == "")
				{
					rpt.Parameters["Kho"].Value = "TẤT CẢ CÁC KHO";
				}
				else
				{
					rpt.Parameters["Kho"].Value = "KHO: " + drcuahang.SelectedValue + " - " + drcuahang.SelectedItem.Text.ToString();
				}

				Session["Report"] = rpt;
				Response.Redirect("~\\Print.aspx");
			}
			catch (Exception ex)
			{
				return;
			}
		}

		protected void drkhuvuc_SelectedIndexChange(object sender, EventArgs objec)
		{
			Get_Ban(drkhuvuc.SelectedValue);
		}

		private void Get_Ban(string sKhuVuc)
		{
			DataTable dt = new DataTable();
			if (sKhuVuc != "")
				dt = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT vvID, vvName FROM dmvv WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"] + "' AND Status = '1' and FK_NhvvID='" + sKhuVuc + "'");
			else
				dt = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT vvID, vvName FROM dmvv WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"] + "' AND Status = '1'");
			DataRow newR = dt.NewRow();
			newR["vvID"] = "";
			newR["vvName"] = "Tất cả";
			dt.Rows.InsertAt(newR, 0);
			drban.DataSource = dt;
			drban.DataTextField = "vvName";
			drban.DataValueField = "vvID";
			drban.DataBind();
			drban.SelectedIndex = 0;
		}

		private DataTable Get_NhomHang()
		{
			DataTable dt = new DataTable();
			dt = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT Nhvt2ID, Nhvt2Name FROM dmnhvt2 WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"] + "' AND NL_yn = 1 AND TP_yn = 0 AND Status = '1'");
			DataRow newR = dt.NewRow();
			newR["Nhvt2ID"] = "";
			newR["Nhvt2Name"] = "Tất cả";
			dt.Rows.InsertAt(newR, 0);
			return dt;
		}

		private DataTable Get_CuaHang()
		{
			DataTable dt = new DataTable();
			dt = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT KhoID, KhoName FROM dmkho WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"] + "' AND Status = '1'");

			DataRow newR = dt.NewRow();
			newR["KhoID"] = "";
			newR["KhoName"] = "Tất cả";
			dt.Rows.InsertAt(newR, 0);
			return dt;
		}

		private DataTable Get_KhuVuc()
		{
			DataTable dt = new DataTable();
			dt = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT NhvvID, NhvvName FROM dmnhvv WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"] + "' AND Status = '1'");
			DataRow newR = dt.NewRow();
			newR["NhvvID"] = "";
			newR["NhvvName"] = "Tất cả";
			dt.Rows.InsertAt(newR, 0);
			return dt;
		}
	}
}
